package com.say.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.say.bean.domain.User;
import com.say.dao.UserDao;
import com.say.framework.core.common.service.BaseServiceImpl;
import com.say.framework.core.persistence.DynamicSpecifications;
import com.say.framework.core.persistence.SearchFilter;
import com.say.framework.idworker.IdWorker;
import com.say.service.UserService;
import com.say.utils.weixin.MUDload;

@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl<User, String> implements UserService {
	
	private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserDao userDao;
	
	@Override
	public PagingAndSortingRepository<User, String> getDao() {
		return userDao;
	}
	
	@Override
	public Page<User> findByPage(Map<String, Object> params, Pageable sp)
			throws Exception {
		 Map<String, SearchFilter> filters = SearchFilter.parse(params);
	     Specification<User> specification = DynamicSpecifications.bySearchFilter(filters.values(), User.class);
	     return userDao.findAll(specification, sp);
	}

	@Override
	public User getUserByOpenId(String openId) {
		return userDao.getUserByOpenId(openId);
	}

	@Override
	public User insertUser(User user) {
		logger.info("insertUser--user)" +JSON.toJSONString(user));
		if(null!=user.getUnionid()){
			 User weiXinUser =userDao.getUserByOpenId(user.getOpenId());
			if(user.getSex()!=null)weiXinUser.setSex(user.getSex());
			if(user.getCity()!=null)weiXinUser.setCity(user.getCity());
			if(user.getCountry()!=null)weiXinUser.setCountry(user.getCountry());
			if(user.getProvince()!=null)weiXinUser.setProvince(user.getProvince());
			if(user.getLanguage()!=null)weiXinUser.setLanguage(user.getLanguage());
			if(user.getWxName()!=null)weiXinUser.setWxName(user.getWxName());
			if(user.getOpenId()!=null){
				if(StringUtils.isBlank(weiXinUser.getOpenId()) ){
					weiXinUser.setOpenId(user.getOpenId());
				}
			}
			weiXinUser.setWxName(user.getWxName());
			if(user.getWeixinPhoto() !=null&&user.getWeixinPhoto().trim().length()>0){
				weiXinUser.setWeixinPhoto(user.getWeixinPhoto());
				String photo  = MUDload.downloadImage(user.getWeixinPhoto());
				if(photo !=null){
					weiXinUser.setPhoto(photo);
				}
			}
			this.save(weiXinUser);
			return weiXinUser;
		}
		if(user.getWeixinPhoto()!=null){
			String photo  = MUDload.downloadImage(user.getWeixinPhoto());
			if(photo !=null){
				user.setPhoto(photo);
			}
		}
		user.setId(IdWorker.generateId());
		this.save(user);
		return user;
	}


	@Override
	public String getOpenId(String id) {
		return userDao.getOpenId(id);
	}

	@Override
	public List<User> findFriendsByUserId(String userId) {
		return userDao.findFriendsByUserId(userId);
	}

	@Override
	public void updateMoodById(String userId, String mood) {
		userDao.updateMoodById(userId, mood);
	}

	@Override
	public User getUserByUserName(String userName) {
		return userDao.getUserByUserName(userName);
	}

}





